home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 04 - 1988 / 04.02 Feb 88 / c postcard / PaintDisplay.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-10-13  |  2.9 KB  |  133 lines  |  [TEXT/KAHL]

  1. /*
  2.  * PaintDisplay.c
  3.  *
  4.  * This program supplies the code needed for the PostCard application
  5.  * to generate a stand alone Paint file.
  6.  *
  7.  * copyright 1987, Joel McNamara - All Rights Reserved
  8.  * for MacTutor Magazine
  9.  *
  10.  * initial coding - October 12, 1987
  11.  *
  12.  */
  13.   
  14. #include    <MacTypes.h>
  15. #include    <QuickDraw.h>
  16. #include    <WindowMgr.h>
  17. #include    <EventMgr.h>
  18. #include    <MemoryMgr.h>
  19. #include    <SegmentLdr.h>
  20.  
  21. /* setup the world, display the image, and wait for a click */
  22. main()
  23. {
  24.     InitGraf(&thePort);
  25.     InitFonts();
  26.     InitWindows();
  27.     InitMenus();
  28.     TEInit();
  29.     InitDialogs(0L);
  30.     InitCursor();
  31.     FlushEvents(everyEvent,0);
  32.  
  33.     displayPaint();
  34.     while (!Button());
  35. }
  36.  
  37.  
  38. /* the guts of the paint display program */
  39. displayPaint()
  40. {
  41. char        *srcPtr, *dstPtr, *saveDstPtr, *skipPtr;
  42. int        srcFile, scanLine, vRef, temp;
  43. long        srcSize, paintSize;
  44. Str255    thisProgram;
  45. StringPtr    thisVolume;
  46. GrafPort    aPort;
  47. BitMap    theBitMap;
  48. Rect        myRect;
  49. WindowPtr    myWind;
  50. Handle    thisHandle;
  51.  
  52.     /* we'll be sloppy with the nonrelocatable pointers and let them
  53.        all get cleaned up upon exiting */
  54.        
  55.     /* the first 512 header bytes to skip */
  56.     srcSize = 512;    
  57.     skipPtr = NewPtr(512);
  58.     if (srcPtr == 0L)
  59.         ErrorRoutine(FALSE,0);
  60.      
  61.      /* the biggest it can be */  
  62.     srcPtr = NewPtr(51840);
  63.     if (srcPtr == 0L)
  64.         ErrorRoutine(FALSE,0);
  65.  
  66.     /* see who and where we are... */
  67.     if (GetVol(&thisVolume,&vRef) != noErr)
  68.         ErrorRoutine(FALSE,0);
  69.  
  70.     GetAppParms(&thisProgram,&temp,&thisHandle);
  71.  
  72.     /* then open our data fork and read past the header */
  73.     if (FSOpen(thisProgram,vRef,&srcFile) != noErr)
  74.         ErrorRoutine(TRUE,srcFile);
  75.         
  76.     if (FSRead(srcFile,&srcSize,skipPtr) != noErr)
  77.         ErrorRoutine(TRUE,srcFile);
  78.  
  79.     /* see how big we really are... */
  80.     if (GetEOF(srcFile,&paintSize) != noErr)
  81.         ErrorRoutine(TRUE,srcFile);
  82.  
  83.     paintSize -= 512;
  84.  
  85.     /* now read the rest of the bytes and close */    
  86.     if (FSRead(srcFile,&paintSize,srcPtr) != noErr)
  87.         ErrorRoutine(TRUE,srcFile);
  88.         
  89.     if (FSClose(srcFile) != noErr)
  90.         ErrorRoutine(TRUE,srcFile);
  91.  
  92.     /* get a destination pointer... */
  93.     dstPtr = NewPtr(51840);
  94.     if (dstPtr == 0L)
  95.         ErrorRoutine(FALSE,0);
  96.     
  97.     saveDstPtr = dstPtr;
  98.     scanLine = 1;
  99.     
  100.     /* and start unpacking the compressed Paint data */
  101.     for (scanLine = 1; scanLine <= 720; scanLine++)
  102.         UnpackBits(&srcPtr,&dstPtr,72);
  103.     
  104.     /* make a window */                
  105.     SetRect(&myRect,12,35,500,325);
  106.       myWind = NewWindow(0L,&myRect,"\p",TRUE,1,-1L,FALSE,99);
  107.  
  108.     /* configure our bitmap */
  109.     theBitMap.baseAddr = saveDstPtr;
  110.     theBitMap.rowBytes = 72;
  111.      
  112.     theBitMap.bounds.top = 0;
  113.     theBitMap.bounds.left = 0;
  114.     theBitMap.bounds.bottom = 72 * 8;
  115.     theBitMap.bounds.right = 720;
  116.  
  117.     /* and copy the Paint bits into our window */      
  118.     CopyBits(&theBitMap, &myWind->portBits, &myWind->portRect,
  119.                         &myWind->portRect, srcCopy, 0L);                        
  120. }
  121.  
  122.  
  123. /* simple no frills, beep and quit */
  124. ErrorRoutine( closeFile, fileNum )
  125. Boolean    closeFile;
  126. int        fileNum;        
  127. {
  128.     SysBeep(5);
  129.     if (closeFile)
  130.         FSClose(fileNum);        
  131.     ExitToShell();
  132. }
  133.